﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using Common;
using Microsoft.WindowsMobile.PocketOutlook;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace MLogix
{
    class MessageEditPanel
    {
        const string SMSFILTER = "SMS Filter";
        static int Width = StedySoft.SenseSDK.DrawingCE.Resolution.ScreenWidth;
        static int Height = StedySoft.SenseSDK.DrawingCE.Resolution.ScreenHeight;
        private System.ComponentModel.IContainer components = null;
        ITranslucentForm form = new ITranslucentForm();
        System.Windows.Forms.TextBox textReadReply;
        System.Windows.Forms.Label senseReplyLabel;
        SenseButtonControl sBttnSend;
        SenseButtonControl sBttnCancel;
        SenseButtonControl sBttnDelete;
        SensePanelItem itm;
        
        Microsoft.WindowsCE.Forms.InputPanel inputPanel1;
        string SenderName = string.Empty;
        string SenderAddress = string.Empty;
        string message = string.Empty;
        string smsAction = "NA";
        bool isBigScreen = false;
        public MessageEditPanel(object Sender, out string action)
        {
            form.Closing += new System.ComponentModel.CancelEventHandler(form_Closing);
            form.Closed += new EventHandler(form_Closed);
            components = new System.ComponentModel.Container();
            isBigScreen = StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA;
            smsAction = "NA";
            if (Sender is SensePanelItem)
            {
                itm = (Sender as SensePanelItem);
                SenderName = itm.Tag.ToString();
                SenderAddress = DBOPS.SenseMessagesDB.GetContactsNumber(SenderName.Trim());
                message = itm.PrimaryText;
                ReplySetting();
                form.ShowDialog();
               
            }
            else if (Sender is string)
            {
                string info = Sender as string;
                SenderAddress = info.Split('|').GetValue(0).ToString();
                SenderName = info.Split('|').GetValue(1).ToString();
                message = "";
                ReplySetting();
                sBttnSend.Text = ReadCustomText.GetString("T_REPLY");
                sBttnSend_Click(null, null);
                form.ShowDialog();
            }
            action = smsAction;    
            
        }

       


        private void ReplySetting()
        {


            textReadReply = new System.Windows.Forms.TextBox();
            senseReplyLabel = new System.Windows.Forms.Label();// SenseLabelControl();
            sBttnSend = new StedySoft.SenseSDK.SenseButtonControl();
            sBttnCancel = new StedySoft.SenseSDK.SenseButtonControl();
            sBttnDelete = new StedySoft.SenseSDK.SenseButtonControl();
            inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
            form.BackColor = StedySoft.SenseSDK.DrawingCE.SenseColors.List_Background;
            form.SuspendLayout();
            // 
            // inputPanel1
            // 
            inputPanel1.Enabled = false;
            //Events
            sBttnCancel.Click += new EventHandler(sBttnCancel_Click);
            sBttnSend.Click += new EventHandler(sBttnSend_Click);
            sBttnDelete.Click += new EventHandler(sBttnDelete_Click);
            // 
            // senseReplyLabel
            // 
            /*senseReplyLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));*/
            senseReplyLabel.Location = new System.Drawing.Point(3, 3);
            senseReplyLabel.Name = "senseReplyLabel";
            senseReplyLabel.Size = new System.Drawing.Size(Width - 6, !isBigScreen ? 40 : 55);
            //TODO: Make Dynamic
            senseReplyLabel.BackColor = StedySoft.SenseSDK.DrawingCE.SenseColors.List_Background;
            senseReplyLabel.Text = SenderName + " " + ReadCustomText.GetString("T_SAYS");
            senseReplyLabel.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
            // 
            // textReadReply
            // 
            textReadReply.AcceptsReturn = true;
            /*textReadReply.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));*/
            textReadReply.BorderStyle = System.Windows.Forms.BorderStyle.None;
            textReadReply.Location = new System.Drawing.Point(3, !isBigScreen ? 45 : 56); // 3+ 16 + <3> 60  20
            textReadReply.Multiline = true;
            textReadReply.Name = "textReadReply";
            textReadReply.ReadOnly = true;
            textReadReply.BackColor = StedySoft.SenseSDK.DrawingCE.SenseColors.List_Background;
            //TODO: Make dynamic
            textReadReply.Text = message;
            textReadReply.Size = new System.Drawing.Size(Width - 6, !isBigScreen ? Height - 65 : Height - 135); //180 + 16 + 3 + 3 = 202
            textReadReply.TabIndex = 0;

            // 
            // sBttnSend
            // 
            /* sBttnSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));*/
            sBttnSend.Location = new System.Drawing.Point(3, !isBigScreen ? Height - 60 : Height - 120);
            sBttnSend.Name = "sBttnReplySend";
            sBttnSend.Size = new System.Drawing.Size((Width / 2) - 3, !isBigScreen ? 28 : 57);
            sBttnSend.TabIndex = 2;
            sBttnSend.Text = ReadCustomText.GetString("T_REPLY");// "Reply";
            // 
            // sBttnCancel
            // 
            /*sBttnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));*/
            sBttnCancel.Location = new System.Drawing.Point((Width / 2) + 6, !isBigScreen ? Height - 60 : Height - 120);
            sBttnCancel.Name = "sBttnCancel";
            sBttnCancel.Size = new System.Drawing.Size((Width / 2) - 9, !isBigScreen ? 28 : 57);
            sBttnCancel.TabIndex = 3;
            sBttnCancel.Text = ReadCustomText.GetString("T_CANCEL");// "Cancel";
            // 
            // sBttnDelete
            // 
            /*sBttnDelete.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));*/
            sBttnDelete.Location = new System.Drawing.Point(3, !isBigScreen ? Height - 30 : Height - 60);
            sBttnDelete.Name = "sBttnDelete";
            sBttnDelete.Size = new System.Drawing.Size(Width - 6, !isBigScreen ? 28 : 57);
            sBttnDelete.TabIndex = 4;
            sBttnDelete.Text = ReadCustomText.GetString("T_DELMESSAGE");//"Delete Message";
            // 
            // ReplyPane
            // 
            form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            form.ClientSize = new System.Drawing.Size(Width, Height);
            form.Location = new System.Drawing.Point(0, 0);
            form.Controls.Add(sBttnDelete);
            form.Controls.Add(sBttnCancel);
            form.Controls.Add(sBttnSend);
            form.Controls.Add(senseReplyLabel);
            form.Controls.Add(textReadReply);
            form.Name = "ReplyPane";
            form.ResumeLayout(false);
        }



        void textReadReply_LostFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = false;
        }

        void textReadReply_GotFocus(object sender, EventArgs e)
        {
            inputPanel1.Enabled = true;
        }

        void sBttnDelete_Click(object sender, EventArgs e)
        {
            if (sBttnDelete.Text.Equals(ReadCustomText.GetString("T_DELMESSAGE")))
            {
                //StedySoft.SenseSDK.Localization.SenseStrings.
                smsAction = "NA";
                if (StedySoft.SenseSDK.DrawingCE.SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_DELTHISSMS"), SMSFILTER, SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (itm != null)
                    {

                        DBOPS.SenseMessagesDB.DeleteSelectedMessage(itm);
                        smsAction = "DEL";
                    }
                    form.Close();
                }
            }
            else
            {
                inputPanel1.Enabled = !inputPanel1.Enabled;
            }
        }

        void sBttnSend_Click(object sender, EventArgs e)
        {
            if (sBttnSend.Text.Equals(ReadCustomText.GetString("T_REPLY")))
            {
                textReadReply.Text = "";
                sBttnSend.Text = ReadCustomText.GetString("T_SEND");
                senseReplyLabel.Text = ReadCustomText.GetString("T_REPLYTO") + " " + SenderAddress;
                textReadReply.ReadOnly = false;
                textReadReply.BackColor = StedySoft.SenseSDK.DrawingCE.SenseColors.List_Background;
                sBttnDelete.Text = ReadCustomText.GetString("T_SIP");
                senseReplyLabel.Refresh();
                textReadReply.GotFocus += new EventHandler(textReadReply_GotFocus);
                textReadReply.LostFocus += new EventHandler(textReadReply_LostFocus);
            }
            else
            {
                inputPanel1.Enabled = false;
                textReadReply.GotFocus -= new EventHandler(textReadReply_GotFocus);
                textReadReply.LostFocus -= new EventHandler(textReadReply_LostFocus);
                //Add sms code
                SMS sms = new SMS(textReadReply.Text, SenderAddress, SenderName);
                Thread th = new Thread(new ThreadStart(sms.SendSMS));
                th.Start();
                form.Close();
                smsAction = "SEN";
                //th.Join();
                //if (!sms.Result)
                //    SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_SMSERROR"), "SMS Filter", SenseMessageBoxButtons.OK);
                //else
                //{
                //    SaveSentMessage();
                //    if (!string.IsNullOrEmpty(ReadCustomText.GetString("T_SMSSENT")))
                //        SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_SMSSENT"), "SMS Filter", SenseMessageBoxButtons.OK);
                //}
            }
        }

        void sBttnCancel_Click(object sender, EventArgs e)
        {
            form.Close();
        }

       
     
        void form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            form.Controls.Clear();

        }
        void form_Closed(object sender, EventArgs e)
        {
            

        }

       

       
    }
    public class SMS
    {
        public delegate void SendDelegate(bool sent);
        public static event SendDelegate messageSent;
        public static string SMSFILTER = "SMS Filter";
        string message = string.Empty;
        string number = string.Empty;
        string name = string.Empty;
        bool result = false;

        public bool Result { get { return result; } }
        public SMS(string message, string number, string name)
        {
            this.message = message;
            this.number = number;
            this.name = name;
        }
       

        public void SendSMS()
        {
            
            try
            {
                using (OutlookSession session = new OutlookSession())
                {
                    SmsMessage SMS = new SmsMessage();
                    SMS.Body = message;
                    SMS.To.Add(new Recipient(number));
                    //SMS.RequestDeliveryReport = NotificationControl.GetDeliveryMode();
                    session.SmsAccount.Send(SMS);
                }
                result = true;
                SaveSentMessage();
                try
                {
                    if (!string.IsNullOrEmpty(ReadCustomText.GetString("T_SMSSENT")) && NotificationControl.GetDeliveryMode())
                        SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_SMSSENT"), SMSFILTER, SenseMessageBoxButtons.OK);
                }
                catch (Win32Exception e)
                {
                    MessageBox.Show(ReadCustomText.GetString("T_SMSSENT"), SMSFILTER);
                }
               
                
            }
            catch
            {
                result = false;
                try
                {
                    SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_SMSERROR"), SMSFILTER, SenseMessageBoxButtons.OK);
                }
                catch (Win32Exception e)
                {
                    MessageBox.Show(ReadCustomText.GetString("T_SMSERROR"), SMSFILTER);
                }
                finally
                {
                    messageSent(false);
                }
                //SenseAPIs.SenseMessageBox.Show(ReadCustomText.GetString("T_SMSERROR"), "SMS Filter", SenseMessageBoxButtons.OK);
            }
        }
        private void SaveSentMessage()
        {
            if (NotificationControl.GetSaveSentMode())
            {
                DBOPS.SenseMessagesDB.SaveSentSMS(CryptLib.SimpleConfuscator(name.Trim()), CryptLib.SimpleConfuscator(number.Trim()), CryptLib.SimpleConfuscator(message), DateTime.Now.ToString());
                messageSent(true);
            }

        }
    }
}
